package aom.example.security.controller;

import aom.example.security.domain.SysMenuEntity;
import aom.example.security.domain.vo.MenuVo;
import aom.example.security.service.SysMenuService;
import bom.example.common.common.Results;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author X.I.O
 * @title: MenuController
 * @projectName springboot
 * @description: TODO
 * @date 2021/10/27 19:01
 */
@Api(tags = {"菜单服务"})
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @ApiOperation("菜单列表")
    @PostMapping("/getMenulist")
    public JSONObject menuListPageController(@ApiParam("页码") @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                             @ApiParam("条数")@RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                                             @ApiParam("关键字")@RequestParam(value = "keyword",required = false)String keyword){
        LambdaQueryWrapper<SysMenuEntity> lambdaQueryWrapper=new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(keyword)){
            lambdaQueryWrapper.or().like(SysMenuEntity::getName,keyword);
            lambdaQueryWrapper.or().like(SysMenuEntity::getPermission,keyword);
        }
        lambdaQueryWrapper.orderByDesc(SysMenuEntity::getCreateDate);
        Page page=new Page(pageNum,pageSize);
        IPage<SysMenuEntity> sysMenuEntityIPage=sysMenuService.page(page,lambdaQueryWrapper);
        return Results.success(sysMenuEntityIPage.getRecords(),sysMenuEntityIPage.getTotal());
    }

    @ApiOperation("新增菜单")
    @PostMapping("/addmenu")
    public JSONObject addMenuController(@RequestBody MenuVo menuVo){
        SysMenuEntity sysMenuEntity=new SysMenuEntity();
        BeanUtils.copyProperties(menuVo,sysMenuEntity);
        sysMenuService.save(sysMenuEntity);
        return Results.success("新增成功");
    }

    @ApiOperation("菜单与角色")
    @PostMapping("menurole")
    public JSONObject menuController(@ApiParam("页码")@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                     @ApiParam("条数")@RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                                     @ApiParam("关键字")@RequestParam(value = "keyword",required = false)String keyword){
        List<Map<String, Object>> objectMaps=sysMenuService.selectRoleMenuList(keyword,pageNum,pageSize);
        Integer count=sysMenuService.selectRoleMenuListCount(keyword);
        return Results.success(objectMaps,count);
    }


}
